﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace 实验六GDI_
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        int b = 0;
        private void 点ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            b = 1;
        }

        private void 点ToolStripMenuItem_Paint(object sender, PaintEventArgs e)
        {
     
        }

        private void 线ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            b = 2;
        }

        private void 多边形ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            b = 3;
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            if(b==1)
            {
                Graphics g = e.Graphics;
                SolidBrush sb = new SolidBrush(Color.Black);
                foreach(Point pt in lp)
                {
                    g.FillEllipse(sb, pt.X, pt.Y, 4, 4);
                    
                }
                sb.Dispose();
                

            }
            if (b == 2)
            {
                Graphics g = e.Graphics;
                Pen pen = new Pen(Color.Red, 2);
                foreach (Myline my in ml)
                {
                    g.DrawLine(pen, templine.start.X, templine.start.Y, templine.end.X, templine.end.Y);
                }
                pen.Dispose();
            }
            if (b == 3)
            {
                Graphics g = e.Graphics;
                Pen pen = new Pen(Color.Red, 2);

                foreach (DBX tempbx in dbx1)
                {
                    g.DrawPolygon(pen, tempbx.dbx.ToArray());
                }
                
            }
 
        }
        List<Point> lp =new List<Point>();
        List<Myline> ml = new List<Myline>();
        List<DBX> dbx1= new List<DBX>();
        //DBX tempdbx;
        DBX tempdbx = new DBX();
        Myline templine;
        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            if (b == 1)
            {
                Point pt = new Point();
                pt.X = e.X;
                pt.Y = e.Y;
                lp.Add(pt);
                Invalidate();
            }
            if(b==2)
            {
                templine = new Myline();
                templine.start.X = e.X;
                templine.start.Y = e.Y;
                //Invalidate();
            }

            if (b == 3 & e.Button == MouseButtons.Left)
            {
                Point pt = new Point();
                //Point pt = new Point();
                pt.X = e.X;
                pt.Y = e.Y;
                //    tempdbx = new DBX();
                tempdbx.dbx.Add(pt);
            }

            if (b==3&&e.Button == MouseButtons.Right)
            {
                dbx1.Add(tempdbx);
                tempdbx = new DBX();
                Invalidate();
            }

            
        }
        class Myline
        {
            public Point start;
            public Point end;
            public Myline()
            {
                start = new Point();
                end = new Point();
            }
            
            
        }
        class DBX
        {
           
            public List<Point> dbx;
            public DBX()
            {
                dbx = new List<Point>();
            }
        }

        private void Form1_MouseUp(object sender, MouseEventArgs e)
        {
            if (b == 2)
            {
                // templine = new Myline();
                templine.end.X = e.X;
                templine.end.Y = e.Y;
                ml.Add(templine);
                Invalidate();
            }


            /* if (b==3&e.Button == MouseButtons.Left)
             {
                 Point pt = new Point();
                 //Point pt = new Point();
                 pt.X = e.X;
                 pt.Y = e.Y;
             //    tempdbx = new DBX();
                 tempdbx.dbx.Add(pt);
             }
          /*   if (e.Button == MouseButtons.Right)
             {
                 dbx1.Add(tempdbx);
                 tempdbx = new DBX();
                 Invalidate();
             }*/
            
        }

        private void Form1_MouseClick(object sender, MouseEventArgs e)
        {
           /* if (b == 3 & e.Button == MouseButtons.Left)
            {
                Point pt = new Point();
                //Point pt = new Point();
                pt.X = e.X;
                pt.Y = e.Y;
                //    tempdbx = new DBX();
                tempdbx.dbx.Add(pt);
            }*/
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}
